<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of category
 *
 * @author wps2000
 */

class CategoryModel extends Db_Base {
    private $_cate  = array();
    private $_list  = array();
    private $_brand  = array();
    private $_area  = array();
    private $_code  = array();

    const Cache_KEY = 'Product_DH1_Category_CACHE';
    const Cache_num_KEY = 'Product_Num_CACHE';
    const Cache_Product_KEY = 'Product_CACHE';

    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    /**
     * 获取分类详情
     * @return array
     */
    public function getCates()
    {
        if (empty($this->_cate))
        {
            return array();
        }
        if(!isset($this->_cate)) return array();

        return $this->_cate;
    }

    /**
     * 这是获取分类下的下单的总吨数
     * @param $cate_id
     */
    public function getProductNum($cate_id){
        $data= $this->_cache_handler->load(self::Cache_num_KEY);
        if(empty($data)){
            $data = $this->_db_handler->fetchOne("select sum(`sos`.`num`) as `numtotal` from `sale_order_sku` as `sos` left join `product` as `p` on(`p`.`id` = `sos`.`product_id`)  WHERE `p`.`cate_id` = '{$cate_id}'");
            $this->_cache_handler->put(self::Cache_num_KEY,$data);
        }
        return $data;
    }

    /**
     * @param $cate_id
     * @param $offset1
     * @param $offset2
     * @return array|mixed
     */
    public function getProductInfo($cate_id,$offset1,$offset2){
        $this->_list= $this->_cache_handler->load(self::Cache_Product_KEY);
        if (empty($this->_list))
        {
            $this->_list = $this->_loadProducyInfo($cate_id,$offset1,$offset2);
        }
        return $this->_list;
    }

    /**
     * @param $cate_id
     * @param $offset1
     * @param $offset2
     * @return mixed
     */
    protected function _loadProducyInfo($cate_id,$offset1,$offset2){
        //先查出这个分类的所有产品,我这边暂时读取100条，不然数据会很慢
        if(!empty($offset1)){
            $product = $this->_db_handler->fetchAll("select `id`,`cate_id` from `product` as `p` where `p`.`cate_id` = '{$cate_id}' limit 0,{$offset1}");
        }else{
            $product = $this->_db_handler->fetchAll("select `id`,`cate_id` from `product` as `p` where `p`.`cate_id` = '{$cate_id}'");
        }

        //这边查询出所有当前分类下,对应产品的报价
        if(!empty($product)){
            foreach($product as $key=>$row){
                if(!empty($offset2)) {
                    $quotation = $this->_db_handler->fetchAll("select `product_id`,`area_id` from `iq_quotation` as `iq` where `iq`.`product_id` = '{$row['id']}' AND `iq`.`status` = '2' AND `iq`.`area_id` <>'' AND `iq`.`currency_id` <> '' limit 0,{$offset2}");
                }else{
                    $quotation = $this->_db_handler->fetchAll("select `product_id`,`area_id` from `iq_quotation` as `iq` where `iq`.`product_id` = '{$row['id']}' AND `iq`.`status` = '2' AND `iq`.`area_id` <>'' AND `iq`.`currency_id` <> '' ");
                }
                //帅选过滤到没有发生报价的产品
                if(empty($quotation)){
                    unset($product[$key]);
                }else{
                    foreach($quotation as $k=>$v){
                        $data[$key]['brand_name'] = $this->_db_handler->fetchRow("select `pb`.`brand_name` from `product_brands` as `pb` left join `product` as `p` on(`pb`.`id` = `p`.`brand_id`) where `p`.`id` = '{$v['product_id']}'");
                        $data[$key]['product_name'] = $this->_db_handler->fetchRow("select `p`.`product_name` from `product` as `p` where `p`.`id` = '{$v['product_id']}'");
                        $data[$key]['area_name'] = $this->_db_handler->fetchRow("select `r`.`region_name` from `region` as `r` where `r`.`region_id` = '{$v['area_id']}'");
                    }
                }

            }
        }
        //组合，去重
        if(!empty($data)){
            foreach($data as $kem=>$vem){
                if(!empty($vem['brand_name'])){
                    $temparr['brand_name'][] = $vem['brand_name']['brand_name'];
                }else{
                    $temparr['brand_name']=array();
                }

                if(!empty($vem['product_name'])){
                    $temparr['product_name'][] = $vem['product_name']['product_name'];
                }else{
                    $temparr['product_name']=array();
                }

                if(!empty($vem['area_name'])){
                    $temparr['area_name'][] = $vem['area_name']['region_name'];
                }else{
                    $temparr['area_name']=array();
                }
            }
        }
        if(!empty($temparr['brand_name'])){
            $temparr['brand_name']= array_unique($temparr['brand_name']);
        }else{
            $temparr['brand_name']= "";
        }

        if(!empty($temparr['product_name'])){
            $temparr['product_name']= array_unique($temparr['product_name']);
        }else{
            $temparr['product_name']= "";
        }

        if(!empty($temparr['area_name'])){
            $temparr['area_name']=array_unique($temparr['area_name']);
        }else{
            $temparr['area_name']= "";
        }

        return $temparr;
    }


    protected function _load()
    {
        $this->_cate    = $this->_cache_handler->load(self::Cache_KEY);
        if (empty($this->_cate))
        {
            $this->_loadFromDB();
        }
    }

    protected function _loadFromDB()
    {
        $tmp_arr = $this->_db_handler->fetchAll("SELECT `id` as `cate_id`, `cate_name`,`vice_name`, `parent_id`, `level`, `is_show`, `index_show` FROM `product_cates` where `index_show` = 'Y' ORDER BY `cates_order` ASC limit 0,5");

        foreach ($tmp_arr as $itm)
        {
            $this->_cate[$itm['cate_id']]['cate_id']            = $itm['cate_id'];
            $this->_cate[$itm['cate_id']]['cate_name']          = $itm['cate_name'];
            $this->_cate[$itm['cate_id']]['vice_name']          = $itm['vice_name'];
            $this->_cate[$itm['cate_id']]['parent_id']          = $itm['parent_id'];
            $this->_cate[$itm['cate_id']]['level']              = $itm['level'];
            $this->_cate[$itm['cate_id']]['is_show']              = $itm['is_show'];
            $this->_cate[$itm['cate_id']]['index_show']              = $itm['index_show'];
        }
        $this->_cache_handler->put(self::Cache_KEY, $this->_cate);
    }

}
?>